package ac.beiwo.baas.admin.controller;

import ac.beiwo.baas.admin.entity.Field;
import ac.beiwo.baas.admin.service.FieldService;
import ac.beiwo.baas.common.entity.SimpleError;
import ac.beiwo.baas.common.entity.SimpleResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static ac.beiwo.baas.common.entity.SimpleResult.success;

/**
 * Created by Staryet on 15/6/15.
 */
@RestController
@RequestMapping(value = "/admin/clazz")
public class FieldController {

    @Autowired
    private FieldService fieldService;

    @RequestMapping(value = "/{clazzName}/field", method = RequestMethod.POST)
    public SimpleResult insert(@PathVariable String clazzName, @RequestBody Field field) throws SimpleError {
        fieldService.insert(clazzName, field);
        return success();
    }

    @RequestMapping(value = "/{clazzName}/field/{name}", method = RequestMethod.DELETE)
    public SimpleResult delete(@PathVariable String clazzName, @PathVariable String name) throws SimpleError {
        fieldService.delete(clazzName, name);
        return success();
    }

    @RequestMapping(value = "/{clazzName}/field/{name}", method = RequestMethod.GET)
    public Field get(@PathVariable String clazzName, @PathVariable String name) throws SimpleError {
        return fieldService.get(clazzName, name);
    }

    @RequestMapping(value = "/{clazzName}/field", method = RequestMethod.GET)
    public List<Field> list(@PathVariable String clazzName) throws SimpleError {
        return fieldService.list(clazzName);
    }

}
